package com.kyzh.core.uis

import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView


class DividerItemDecoration : RecyclerView.ItemDecoration {
    private var mDivider: Drawable? = null
    private var mPaddingLeft = 0
    constructor(divider: Drawable?):super(){
        mDivider = divider
        mPaddingLeft = 0
    }

    constructor (divider: Drawable?, paddingLeft: Int) :super(){
        mDivider = divider
        mPaddingLeft = paddingLeft
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        if (mDivider == null) return
        if (parent.getChildAdapterPosition(view) < 1) return
        if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
            outRect.top = mDivider!!.getIntrinsicHeight()
        } else {
            outRect.left = mDivider!!.getIntrinsicWidth()
        }
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (mDivider == null) {
            super.onDrawOver(c, parent, state!!)
            return
        }
        if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
            val left = parent.paddingLeft + mPaddingLeft
            val right = parent.width - parent.paddingRight - 40
            val childCount = parent.childCount
            for (i in 1 until childCount) {
                val child: View = parent.getChildAt(i)
                val params = child.getLayoutParams() as RecyclerView.LayoutParams
                val size: Int = mDivider!!.getIntrinsicHeight()
                val top: Int = child.getTop() - params.topMargin
                val bottom = top + size
                mDivider!!.setBounds(left, top, right, bottom)
                mDivider!!.draw(c)
            }
        } else { //horizontal
            val top = parent.paddingTop
            val bottom = parent.height - parent.paddingBottom
            val childCount = parent.childCount
            for (i in 1 until childCount) {
                val child: View = parent.getChildAt(i)
                val params = child.getLayoutParams() as RecyclerView.LayoutParams
                val size: Int = mDivider!!.getIntrinsicWidth()
                val left: Int = child.getLeft() - params.leftMargin
                val right = left + size
                mDivider!!.setBounds(left, top, right, bottom)
                mDivider!!.draw(c)
            }
        }
    }

    private fun getOrientation(parent: RecyclerView): Int {
        return if (parent.layoutManager is LinearLayoutManager) {
            val layoutManager = parent.layoutManager as LinearLayoutManager?
            layoutManager!!.orientation
        } else throw IllegalStateException("DividerItemDecoration can only be used with a LinearLayoutManager.")
    }
}